package com.ozveri.menu;


import android.content.Context;
import android.graphics.Point;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

public class AnimationFactory {
	
	private int viewNumber;
	private Context ctx;
	private WindowManager WndwMng;
	private int screenWidth;
	private int screenHeight;
	private long DURATION = 2000;
	
	public AnimationFactory(int numberOfViews,Context context, WindowManager windowManager) {
		viewNumber = numberOfViews;
		ctx = context;
		WndwMng = windowManager;
	}


	public Animation setAnimation(AnimatedMainScreenFactory factory, int viewsNumber) {
		LinearLayout layoutToBeAnimated = (LinearLayout) factory.mainButtons.get(viewsNumber);
		Point layoutCoordinates = factory.mainButtonPoints.get(viewsNumber);
		Point layoutMargins = factory.mainButtonPoints.get(viewsNumber);
		if(WndwMng.getDefaultDisplay().getWidth()>WndwMng.getDefaultDisplay().getHeight()){
			screenWidth = WndwMng.getDefaultDisplay().getWidth();
			screenHeight = WndwMng.getDefaultDisplay().getHeight();			
		}
		else
		{
			screenWidth =	WndwMng.getDefaultDisplay().getHeight();
			screenHeight =  WndwMng.getDefaultDisplay().getWidth();	
		}
		
		if((screenHeight-150)/viewNumber<50)
		{
			layoutToBeAnimated.getChildAt(0).setVisibility(View.GONE);
		}
		else
		{
			layoutToBeAnimated.getChildAt(1).setVisibility(View.GONE);
		}
		
		
//		RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(layoutToBeAnimated.getLayoutParams());
//		newParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT; 
//		newParams.width = RelativeLayout.LayoutParams.WRAP_CONTENT; 
//		layoutToBeAnimated.setLayoutParams( newParams);
		
		int newXCoordinate = 0;
		int newYCoordinate = (screenHeight-150)/viewNumber*viewsNumber;		

		AnimationSet animation = new AnimationSet(false);
		TranslateAnimation animationTranslate = new TranslateAnimation(
				TranslateAnimation.ABSOLUTE, 0,
				TranslateAnimation.ABSOLUTE, (newXCoordinate - (layoutCoordinates.x+((factory.mainButtonUsable.get(viewsNumber).x-factory.defaultSize.x)/4))),
				TranslateAnimation.ABSOLUTE, 0, 
				TranslateAnimation.ABSOLUTE, (newYCoordinate - layoutCoordinates.y));
		// animation.setInterpolator()
		ScaleAnimation animationScale = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f);
		animation.addAnimation(animationScale);
		animation.addAnimation(animationTranslate);
		animation.setDuration(DURATION);
		animation.setAnimationListener(new AnimationFactoryListener(layoutToBeAnimated, newXCoordinate, newYCoordinate,factory.defaultSize));
		
		return animation;

	}
	
	
	public class AnimationFactoryListener implements AnimationListener
	{
		LinearLayout layout;
		
		int newXCoordinate;
		int newYCoordinate;	
		Point defaultSize;
		public AnimationFactoryListener(LinearLayout layout, int xCoordinate, int yCoordinate, Point defaultSize) {
			this.layout = layout;
			newXCoordinate = xCoordinate;
			newYCoordinate = yCoordinate;
			this.defaultSize = defaultSize;
 
		}

		public void onAnimationEnd(Animation animation) {
			layout.clearAnimation();
			RelativeLayout.LayoutParams paramsMug = new RelativeLayout.LayoutParams(
					defaultSize.x/2, layout.getHeight()/2);
			paramsMug.setMargins(newXCoordinate, newYCoordinate, 0, 0);
			layout.getChildAt(0).setPadding(0, 5, 0, 5);
			layout.setLayoutParams(paramsMug);
			
		}

		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
			
		}
		
	}

}
